package com.mt.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.common.util.ConvertUtil;
import com.mt.system.api.service.IAdminUserService;
import com.mt.system.entity.AdminRoleRelateEntity;
import com.mt.system.entity.AdminUserEntity;
import com.mt.system.entity.RoleMenuRelateEntity;
import com.mt.system.service.model.AdminRoleRelateServiceModel;
import com.mt.system.service.model.AdminUserServiceModel;
import com.mt.system.service.model.RoleMenuRelateServiceModel;

@Service
public class AdminUserServiceImpl implements IAdminUserService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AdminUserServiceImpl.class);

    @Autowired
    private AdminUserServiceModel adminUserServiceModel;

    @Autowired
    private AdminRoleRelateServiceModel adminRoleRelateServiceModel;

    @Autowired
    private RoleMenuRelateServiceModel adminRoleMenuServiceModel;

    public ServiceResult<AdminUserEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<AdminUserEntity> result = null;
        // 该参数标识前段是否需要权限数据
        boolean withPermission = ConvertUtil.toBool(params.get("withPermission"), true);
        // 登录用户，权限和页面相关展示
        ServiceResult<List<AdminRoleRelateEntity>> adminRoleListResult = null;
        try {
            result = adminUserServiceModel.selectInfo(params);
            if (withPermission && result.getSuccess() && result.getResult() != null) {
                AdminUserEntity adminUserEntity = result.getResult();
                // 查询用户关联角色信息
                Map<String, Object> roleParams = new HashMap<String, Object>();
                // 根据adminUserId确认角色的权限
                roleParams.put("userId", adminUserEntity.getUserId());
                adminRoleListResult = adminRoleRelateServiceModel.selectList(roleParams, null);
                // 得到权限，目录的展示
                if (adminRoleListResult.getSuccess() && adminRoleListResult.getResult() != null
                        && adminRoleListResult.getResult().size() > 0) {
                    List<AdminRoleRelateEntity> adminRoleList = adminRoleListResult.getResult();
                    Map<String, Object> roleMenuParams = null;
                    for (AdminRoleRelateEntity adminRoleRelateEntity : adminRoleList) {
                        // 查询角色权限信息
                        roleMenuParams = new HashMap<String, Object>();
                        roleMenuParams.put("roleId", adminRoleRelateEntity.getRoleId());
                        ServiceResult<List<RoleMenuRelateEntity>> roleMenuListResult = adminRoleMenuServiceModel
                                .selectList(roleMenuParams, null);
                        if (roleMenuListResult.getSuccess() && roleMenuListResult.getResult() != null
                                && roleMenuListResult.getResult().size() > 0) {
                            adminRoleRelateEntity.getRoleEntity().setRoleMenuList(roleMenuListResult.getResult());
                        }
                    }
                    adminUserEntity.setAdminRoleList(adminRoleList);
                }

            }
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<AdminUserEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<AdminUserEntity>> result = new ServiceResult<List<AdminUserEntity>>();
        try {
            result = adminUserServiceModel.selectList(params, pagerInfo);
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> doInsert(AdminUserEntity adminUserEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = adminUserServiceModel.insert(adminUserEntity);
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(AdminUserEntity adminUserEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = adminUserServiceModel.updateRelate(adminUserEntity);
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer userId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = adminUserServiceModel.deleteByPrimaryKey(userId);
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(AdminUserEntity adminUserEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = adminUserServiceModel.updateByPrimaryKeySelective(adminUserEntity);
        } catch (Exception e) {
            LOGGER.error("[AdminUserServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
